AddCSLuaFile("shared.lua")
AddCSLuaFile("cl_init.lua")
AddCSLuaFile("init.lua")
include('shared.lua')
include('init.lua')

ENT.Team = TEAM_BLUE
ENT.Game = nil
ENT.Holder = nil
ENT.FlagBase = nil

function ENT:Initialize()
	self.Entity:SetModel("models/roller.mdl")
	self.Entity:PhysicsInit(SOLID_VPHYSICS)
	self.Entity:SetMoveType(MOVETYPE_NONE)
	
	self.Entity:SetSolid(SOLID_NONE)
	
	util.PrecacheModel("models/roller_spikes.mdl")
	-- no shadow, and don't collide with players
	self.Entity:DrawShadow(false)
end

function ENT:Setupify(nuTeam,gmOwnr)
	self.Team = nuTeam
	self.Game = gmOwnr
	if nuTeam == TEAM_BLUE then
		self.Entity:SetNetworkedBool("blueteam",true)
	else
		self.Entity:SetNetworkedBool("blueteam",false)
	end
end

function ENT:SetBase(base)
	self.FlagBase = base
end

function ENT:Taken(ply)
	self.FlagBase:FlagTaken()
	self.Holder = ply
	ply:SetVar("hasflag",self)
	self.Entity:SetModel("models/roller_spikes.mdl")
	self.Entity:SetPos(ply:GetPos()+Vector(0,0,90))
	self.Entity:SetParent(ply)
end

function ENT:Return()
	self.Entity:SetParent(nil)
	if self.Holder != nil then
		self.Holder:SetVar("hasflag",nil)
		self.Holder = nil
	end
	self.Entity:SetMoveType(MOVETYPE_NONE)
	self.Entity:SetSolid(SOLID_NONE)
	self.Entity:SetTrigger(false)
	self.Entity:SetModel("models/roller.mdl")
	self.Entity:SetPos(self.FlagBase:GetPos()+Vector(0,0,48))
	self.FlagBase:FlagReturned()
end

function ENT:Drop(ply)
	self.Game:FlagDropped(self.Team,ply)
	ply:SetVar("hasflag",nil)
	self.Entity:SetParent(nil)

	local op = self.Entity:GetPos()
	
	self.Entity:SetSolid(SOLID_BBOX)
	self.Entity:SetCollisionGroup(COLLISION_GROUP_WEAPON)
	self.Entity:SetCollisionBounds(Vector(-24,-24,-40),Vector(24,24,24))
	self.Entity:SetTrigger(true)
	self.Entity:SetMoveType(MOVETYPE_VPHYSICS)
	self.Entity:SetPos(op)
	
	local phy = self.Entity:GetPhysicsObject()
	if phy:IsValid() then
		phy:Wake()
		phy:EnableGravity(true)
		phy:EnableDrag(true)
		phy:SetMass(750)
	end

	/*
	if GetGlobalBool("RESET") == true then
		self.Entity:SetParent(nil)
		if self.Holder != nil then
			self.Holder:SetVar("hasflag",nil)
			self.Holder = nil
		end
		self.Entity:SetMoveType(MOVETYPE_NONE)
		self.Entity:SetSolid(SOLID_NONE)
		self.Entity:SetTrigger(false)
		self.Entity:SetModel("models/roller.mdl")
		self.Entity:SetPos(self.FlagBase:GetPos()+Vector(0,0,48))
		self.FlagBase:FlagReturned()
	end
	*/
end

function ENT:Touch(other)
	if other && other:IsValid() && other:IsPlayer() then
		if other:Team() != self.Team then
			other:PrintMessage(HUD_PRINTCENTER,"You have the flag - return to base!")
			self:Pickup(other)
		else
			self:Return()
			self.Game:FlagReturned(self.Team,other)
		end
	end
end

function ENT:PhysicsCollide()
	
	if GetGlobalBool("RESET") == true then
		self.Entity:SetParent(nil)
		if self.Holder != nil then
			self.Holder:SetVar("hasflag",nil)
			self.Holder = nil
		end
		self.Entity:SetMoveType(MOVETYPE_NONE)
		self.Entity:SetSolid(SOLID_NONE)
		self.Entity:SetTrigger(false)
		self.Entity:SetModel("models/roller.mdl")
		self.Entity:SetPos(self.FlagBase:GetPos()+Vector(0,0,48))
		SetGlobalBool("Announce",false)
		self.FlagBase:FlagReturned()
	end

end

function ENT:Reset()
	self.Entity:SetParent(nil)
	if self.Holder != nil then
		self.Holder:SetVar("hasflag",nil)
		self.Holder = nil
	end
	self.Entity:SetMoveType(MOVETYPE_NONE)
	self.Entity:SetSolid(SOLID_NONE)
	self.Entity:SetTrigger(false)
	self.Entity:SetModel("models/roller.mdl")
	self.Entity:SetPos(self.FlagBase:GetPos()+Vector(0,0,48))
	self.FlagBase:ResetTeams()
end

function ENT:Pickup(ply)
	self.Entity:SetSolid(SOLID_NONE)
	self.Entity:SetMoveType(MOVETYPE_NONE)
	self.Entity:SetTrigger(false)
	self.Entity:SetPos(ply:GetPos()+Vector(0,0,90))
	self:Taken(ply)
end